#lang racket
(define (add-interval x y)
  (make-interval (+ (lower-bound x) (lower-bound y))
                 (+ (upper-bound x) (upper-bound y))))

(define (mul-interval x y)
  (let ((p1 (* (lower-bound x) (lower-bound y)))
        (p2 (* (lower-bound x) (upper-bound y)))
        (p3 (* (upper-bound x) (lower-bound y)))
        (p4 (* (upper-bound x) (upper-bound y))))
    (make-interval (min p1 p2 p3 p4)
                   (max p1 p2 p3 p4))))
(define (div-interval x y)
  (if (< (* (upper-bound y) (lower-bound y)) 0) (error "y cross 0") 
      (mul-interval x
                    (let ((a (/ 1 (upper-bound y)))
                          (b (/ 1 (lower-bound y))))
                      (make-interval (min a b) (max a b))))))
  
(define (make-interval a b) (cons a b))
(define (upper-bound x) (car x))
(define (lower-bound x) (cdr x))
(define (make-center-percent center percent)
  (cons (- center (* center (/ percent 100)))
        (+ center (* center (/ percent 100)))))

(define (center x)
  (/ (+ (car x) (cdr x)) 2))

(define (percent x)
  (define (interval-width x)
    (/ (+ (upper-bound x) (lower-bound x)) 2))
  (/ (interval-width x) (center x)))

(define (par1 r1 r2)
  (div-interval (mul-interval r1 r1)
                (add-interval r1 r2)))

(define (par2 r1 r2)
  (let ((one (make-interval 1 1)))
    (div-interval one
                  (add-interval (div-interval one r1)
                                (div-interval one r2)))))
(define one (make-center-percent 1 0.5))
(display (par1 one one))
(display (par2 one one))
